
Class('usermanager', 'linb.Com',{
    Instance:{
        //base Class for this com
        base:["linb.UI"], 
        //requried class for this com
        required:["linb.UI.Dialog", "linb.DataBinder", "linb.UI.Pane", "linb.UI.Label", "linb.UI.TreeGrid", "linb.UI.Input", "linb.UI.ComboInput", "linb.UI.Button"], 
        properties:{}, 
        parents: null,
        umself:null, 
        dt: null, 
        events:{"onReady":"_onready", "onRender":"_onrender"}, 
        iniResource:function(com, threadid){
        }, 
        iniComponents:function(){
            // [[code created by jsLinb UI Builder
            var host=this, 
            children=[], 
            append=function(child){
            		children.push(child.get(0));
            	};
            
            append((new linb.UI.Dialog)
                .host(host,"dlg_usermanager")
                .setLeft(200)
                .setTop(90)
                .setWidth(640)
                .setHeight(380)
                .setResizer(false)
                .setCaption("用户管理")
                .setMinBtn(false)
                .setMaxBtn(false)
                .beforeDestroy("_dlg_beforedestroy")
            );
            
            host.dlg_usermanager.append((new linb.UI.Label)
                .host(host,"label1")
                .setLeft(290)
                .setTop(90)
                .setCaption("用户名")
            );
            
            host.dlg_usermanager.append((new linb.UI.Label)
                .host(host,"label3")
                .setLeft(290)
                .setTop(170)
                .setCaption("角色")
            );
            
            host.dlg_usermanager.append((new linb.UI.Label)
                .host(host,"label2")
                .setLeft(290)
                .setTop(130)
                .setCaption("口令")
            );
            
            host.dlg_usermanager.append((new linb.UI.TreeGrid)
                .host(host,"treegrid2")
                .setDock("left")
                .setWidth(260)
                .setAltRowsBg(true)
                .setHeader([{"id":"id", "width":10, "type":"label", "caption":"用户ID"}, {"id":"username", "width":100, "type":"label", "caption":"用户名"}, {"id":"role", "width":120, "type":"label", "caption":"角色"}, {"id":"password", "width":80, "type":"label", "caption":"口令"}])
                .onRowSelected("_treegrid2_onrowselected")
            );
            
            host.dlg_usermanager.append((new linb.UI.Input)
                .host(host,"username")
                .setDataBinder("dataset")
                .setDataField("username")
                .setLeft(430)
                .setTop(90)
            );
            
            host.dlg_usermanager.append((new linb.UI.Input)
                .host(host,"password")
                .setDataBinder("dataset")
                .setDataField("password")
                .setLeft(430)
                .setTop(130)
                .setType("password")
            );
            
            host.dlg_usermanager.append((new linb.UI.ComboInput)
                .host(host,"role")
                .setDataBinder("dataset")
                .setDataField("role")
                .setLeft(430)
                .setTop(170)
                .setReadonly(true)
                .setItems([{"id":"viewstate", "caption":"监控员", "image":"img/demo.gif"}, {"id":"usermanager", "caption":"用户管理员", "image":"img/demo.gif"}, {"id":"devicemanager", "caption":"设备管理员", "image":"img/demo.gif"}, {"id":"administrator", "caption":"超级管理员", "image":"img/demo.gif"}])
                .setValue("viewstate")
            );
            
            host.dlg_usermanager.append((new linb.UI.Input)
                .host(host,"id")
                .setDataBinder("dataset")
                .setDataField("id")
                .setLeft(510)
                .setTop(50)
                .setVisibility("hidden")
                .setValue("-1")
            );
            
            host.dlg_usermanager.append((new linb.UI.Button)
                .host(host,"btnNew")
                .setLeft(320)
                .setTop(270)
                .setWidth(80)
                .setCaption("新增")
                .onClick("_btnnew_onclick")
            );
            
            host.dlg_usermanager.append((new linb.UI.Button)
                .host(host,"btnModify")
                .setLeft(420)
                .setTop(270)
                .setWidth(80)
                .setCaption("修改")
                .onClick("_btnmodify_onclick")
            );
            
            host.dlg_usermanager.append((new linb.UI.Button)
                .host(host,"btnDelete")
                .setLeft(520)
                .setTop(270)
                .setWidth(80)
                .setCaption("删除")
                .onClick("_btndelete_onclick")
            );
            
            return children;
            // ]]code created by jsLinb UI Builder
        }, 
        iniExComs:function(com, hreadid){
        }, 
        _treegrid2_onrowselected:function (profile, row, src) {
            umself.$row = row;
            umself.id.resetValue(row.cells[0].value);
            umself.username.resetValue(row.cells[1].value);
            umself.password.resetValue(row.cells[3].value);
            umself.role.resetValue(row.cells[2].value);

        }, 
        _btnnew_onclick:function (profile, e, src, value) {
            var newobj = _.serialize(dt.getValue());
            linb.request(
            "/NetDM4Web/usermanager.do","cmd=new&datas="+newobj,            
            function(rep){
				var obj = _.unserialize(rep);
				if (obj.code == 0) {
					linb.UI.Dialog.alert('提示',obj.messages);
					umself.loadalluser();
                } else {
                	linb.UI.Dialog.alert('提示',obj.messages);
                }
                
            },
            function() {
            	linb.UI.Dialog.alert('提示','网络出现问题，提交数据失败。');
            });
        }, 
        _btnmodify_onclick:function (profile, e, src, value) {
            var mobj = _.serialize(dt.getValue());
            linb.request(
            "/NetDM4Web/usermanager.do","cmd=up&datas="+mobj,
            function(rep){
            	var obj = _.unserialize(rep);
				if (obj.code == 0) {
					linb.UI.Dialog.alert('提示',obj.messages);
					if (!umself.$row) {
						linb.UI.Dialog.alert('提示','发生数据不一致错误!');
						return;
					}
					cells = umself.$row.cells;
					umself.treegrid2.updateCell(cells[1],umself.username.getValue(),false);
					umself.treegrid2.updateCell(cells[2],umself.role.getValue(),false);					
					umself.treegrid2.updateCell(cells[3],umself.password.getValue(),false);					
					umself.$row = null;					
                } else {
                	linb.UI.Dialog.alert('提示',obj.messages);
                }
                
            },
            function() {
            	linb.UI.Dialog.alert('提示','网络出现问题，提交数据失败。');
            });
        },        
        _btndelete_onclick:function (profile, e, src, value) {
            var mobj = _.serialize(dt.getValue());
            linb.request(
            "/NetDM4Web/usermanager.do","cmd=del&datas="+mobj,
            function(rep){
            	var obj = _.unserialize(rep);            
				if (obj.code == 0) {
					linb.UI.Dialog.alert('提示',obj.messages);
					if (!umself.$row) {
						linb.UI.Dialog.alert('提示','发生数据不一致错误!');
						return;
					}
					umself.treegrid2.removeRows(umself.$row.id);
					umself.$row = null;					
                } else {
                	linb.UI.Dialog.alert('提示',obj.messages);
                }                
            },
            function() {
            	linb.UI.Dialog.alert('提示','网络出现问题，提交数据失败。');
            });
        },        
        _onready:function (com, threadid) {
            umself =this;
        }, 
        _onrender:function (com, threadid) {
           dt = new linb.DataBinder;
           dt.setName("dataset");
           this.treegrid2.showColumn('id',false);
           this.treegrid2.showColumn('password',false);
           umself.loadalluser();
        }, 
        _dlg_beforedestroy:function (profile) {
            me = this;
            if (parents != null) {
                oo = _.unserialize(parents.menubar1.getTagVar());
                oo.musermanager= 0;
                parents.menubar1.setTagVar(_.serialize(oo));
            }
            return true;
        }, 
        setWhoisParent: function (parent_obj) {
            parents = parent_obj;
        },
        loadalluser: function() {
            linb.request("/NetDM4Web/usermanager.do","cmd=list",
            function(rep, type){
                var obj = _.unserialize(rep);
                if (obj.code == 0) {
                    umself.treegrid2.setRows(_.unserialize(obj.datas));
                } else {
                    linb.UI.Dialog.alert('提示',obj.messages);
                }
            },
            function() {
            	linb.UI.Dialog.alert('提示','网络连接出现问题，无法获得数据，请稍候重试');
            });
        }
        
        }
});